package com.woniu.cq.mallproduct.service.impl;

import com.woniu.cq.mallproduct.client.OrderClient;
import com.woniu.cq.mallproduct.domain.Product;
import com.woniu.cq.mallproduct.mapper.ProductMapper;
import com.woniu.cq.mallproduct.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author daniel
 * @since 2020-09-19
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderClient orderClient;

    @Override
    public void addProduct(Product product) {
        save(product);
    }

    /**
     * @description: 上架
     * @param: productId
     * @return: void
     */
    @Override
    public int upProduct(Integer productId) {
        Product product = getById(productId);
        Boolean productNumber = stringRedisTemplate.opsForHash().hasKey("product:" + productId, "productNumber");
        Boolean productBuyNumber = stringRedisTemplate.opsForHash().hasKey("product:" + productId, "productBuyNumber");
        if (productNumber && productBuyNumber) {
            stringRedisTemplate.opsForHash().increment("product:" + productId, "productNumber", product.getProductNumber());
            stringRedisTemplate.opsForHash().increment("product:" + productId, "productBuyNumber", product.getProductBuyNumber());
        } else {
            stringRedisTemplate.opsForHash().put("product:" + productId, "productNumber", product.getProductNumber() + "");
            stringRedisTemplate.opsForHash().put("product:" + productId, "productBuyNumber", product.getProductBuyNumber() + "");
        }
        /*//修改状态为上架
        Product product1 = new Product();
        product1.setProductId(productId);
        product1.setProductStatus(0);
        int b = productMapper.updateById(product1);*/
        if (true) {
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * @description: 下架
     * @param: productId
     * @return: int
     */
    @Override
    public void downProduct(Integer productId) {
        stringRedisTemplate.opsForHash().delete("product:" + productId, "productNumber");
        stringRedisTemplate.opsForHash().delete("product:" + productId, "productBuyNumber");
        /*Product product1 = new Product();
        product1.setProductId(productId);
        product1.setProductStatus(1);
        productMapper.updateById(product1);*/
    }

    /**
     * @description: 删除
     * @param: productId
     * @return: void
     */
    @Override
    public void deleteProduct(Integer productId) {
        stringRedisTemplate.opsForHash().delete("product:" + productId, "productNumber");
        stringRedisTemplate.opsForHash().delete("product:" + productId, "productBuyNumber");
        Product product = new Product();
        product.setProductId(productId);
        product.setProductStatus(2);
        updateById(product);
    }

    /**
     * @description: 查询上架商品
     * @param:
     * @return: java.util.List<com.woniu.cq.mallproduct.domain.Product>
     */
    @Override
    public List<Product> getProduct() {
        Set<String> keys = stringRedisTemplate.keys("*");
        System.out.println(keys);
        List<Product> products = new ArrayList<>();
        for (String productId : keys) {
            if (productId.contains("product:")) {
                productId = productId.split(":")[1];
                int id = Integer.parseInt(productId);
                Product product = getById(id);
                products.add(product);
            }
        }
        return products;
    }

    /**
     * @description: 单一商品
     * @param: productId
     * @return: com.woniu.cq.mallproduct.domain.Product
     */
    @Override
    public Product getOneProduct(Integer productId) {
        Product product = getById(productId);
        return product;
    }

    /**
     * @description: 模糊查询
     * @param: name
     * @param: info
     * @return: java.util.List<com.woniu.cq.mallproduct.domain.Product>
     */
    @Override
    public List<Product> getLikeProduct(String name, String info) {
        List<Product> likeProduct = productMapper.getLikeProduct(name, info);
        List<Product> products = new ArrayList<>();
        for (Product p : likeProduct){
            Integer productId = p.getProductId();
            if (stringRedisTemplate.opsForHash().hasKey("product:"+productId,"productNumber")){
                products.add(p);
            }
        }
        return products;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int buy(Integer userId) {
        Set<Object> keys = stringRedisTemplate.opsForHash().keys("userId:" + userId);
        for (Object k : keys) {
            System.out.println(k + "--------------------");
            Integer number = Integer.parseInt(stringRedisTemplate.opsForHash().get("userId:" + userId, k).toString());
            System.out.println(number + "------------------");
            Integer productNumber = Integer.parseInt(stringRedisTemplate.opsForHash().get("product:" + (k.toString().split(":")[1]), "productNumber").toString());
            System.out.println(productNumber + "--------------------");
            if (productNumber >= number) {
                //减库存
                stringRedisTemplate.opsForHash().increment("product:" + (k.toString().split(":")[1]), "productNumber", -number);
                //加购买量
                stringRedisTemplate.opsForHash().increment("product:" + (k.toString().split(":")[1]), "productBuyNumber", number);
                //新增订单
                int orderId = orderClient.createOrder(userId);
                return orderId;
            }
            //库存不足
            return -1;
        }
        return -1;
    }

    @Override
    public void addCourt(Integer productId, Integer productNumber, Integer userId) {
        Boolean aBoolean = stringRedisTemplate.opsForHash().hasKey("userId:" + userId, "productId:" + productId);
        if (aBoolean) {
            stringRedisTemplate.opsForHash().increment("userId:" + userId, "productId:" + productId, productNumber);
        } else {
            stringRedisTemplate.opsForHash().put("userId:" + userId, "productId:" + productId, productNumber+"");
        }
    }

    @Override
    public List<Product> getProducts() {
        List<Product> productList = productMapper.getProducts();
        return productList;
    }


}
